package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.MarketBo;
import org.dromara.web.domain.vo.MarketVo;
import org.dromara.web.service.impl.ApiServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.dromara.common.core.domain.R;

import java.util.List;

@Validated
@RestController
@SaIgnore
@RequiredArgsConstructor
@RequestMapping("/api/stock")
@Slf4j
public class ApiController extends BaseController {

    @Lazy
    private final ApiServiceImpl apiService;

    /**
     * 手动调用接口
     *
     * @return 操作结果
     * @throws InterruptedException 如果线程被中断
     *                              抛出异常
     */

    @SaCheckPermission("market:market:call")
    @GetMapping("/getStock")
    public R<String> getStock() throws InterruptedException {
        String stock = apiService.getStock();
        return R.ok("调用Api成功",stock);
    }


    /**
     * 添加计数器清零接口
     *
     * @return 操作结果
     */
    @SaCheckPermission("market:market:resetStartPage")
    @PostMapping("/resetStartPage")
    public R<String> resetStartPage() {
        apiService.resetStartPage();
        return R.ok("计时器重置成功");
    }


    /**
     * 查询Market信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return Market分页列表
     */
    @SaCheckPermission("market:market:list")
    @GetMapping("/marketList")
    @Tool("查询股票信息列表")
    public TableDataInfo<MarketVo> list(@P("查询条件") MarketBo bo, @P("分页参数") PageQuery pageQuery,
                                        @RequestParam(required = false) String sortField,
                                        @RequestParam(required = false) String sortOrder) {
        bo.setSortField(sortField);
        bo.setSortOrder(sortOrder);
        return apiService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取Market信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("market:market:query")
    @SaIgnore
    @GetMapping("/{id}")
    @Tool("根据id查询股票信息")
    public R<MarketVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(apiService.queryById(id));
    }

    /**
     * 新增Market信息
     */
    @SaCheckPermission("market:market:add")
    @SaIgnore
    @Log(title = "Market信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MarketBo bo) {
//        return toAjax(apiService.insertByBo(bo));
        return null;
    }

    /**
     * 修改Market信息
     */
    @SaCheckPermission("market:market:edit")
    @SaIgnore
    @Log(title = "Market信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MarketBo bo) {
        return toAjax(apiService.updateByBo(bo));
    }

    /**
     * 删除Market信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("market:market:remove")
    @SaIgnore
    @Log(title = "Market信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(apiService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取所有股票代码
     *
     * @return 股票代码列表
     */
    @SaCheckPermission("market:market:getSymbols")
    @SaIgnore
    @Log(title = "Market信息", businessType = BusinessType.INSERT)
    @GetMapping("/symbols")
    public R<List<String>> listSymbols() {
        return R.ok(apiService.listSymbols());
    }


    /**
     * 根据 symbol 查询股票信息列表（分页参数固定）
     *
     * @param symbol    股票代码
     * @param sortField 排序列（可选）
     * @param sortOrder 排序方向（可选）
     * @return Market 分页列表
     */
    @SaCheckPermission("market:market:list")
    @GetMapping("/marketSymbolList/{symbol}")  // 移除路径中的 {pageSize} 和 {pageNum}
    @Tool("分析股票代码symbol近期走势")
    public TableDataInfo<MarketVo> listBySymbol(
        @P("股票代码")@PathVariable String symbol,
        @RequestParam(required = false) String sortField,
        @RequestParam(required = false) String sortOrder) {
        MarketBo bo = new MarketBo();
        bo.setSymbol(symbol);
        bo.setSortField(sortField);
        bo.setSortOrder(sortOrder);
        // 固定分页参数（示例中 pageSize=100，pageNum=1）
        PageQuery pageQuery = new PageQuery(100, 1);  // 直接写死分页值
        // 查询原始数据（time 为 Date 类型）
        TableDataInfo<MarketVo> result = apiService.queryPageList(bo, pageQuery);

        return apiService.queryPageList(bo, pageQuery);
    }


}
